<template>
	<view class="sell">
		<Navbar>出售</Navbar>
		<scroll-view scroll-y class="scroll_view">
			<view class="info">
				<image :src="cover" class="cover"></image>
				<view class="info_view">
					<view class="name">{{name}}</view>
					<view class="number" v-if="number">#{{number}}</view>
				</view>
			</view>
			<view class="content">
				<view class="title">
					<view>
						<image :src="`${baseImageUrl}/common/zw.png`"></image>
						<text>设置价格</text>
						<image :src="`${baseImageUrl}/common/zw.png`"></image>
					</view>
				</view>
				<view class="price">
					<input type="text" placeholder="请输入要转售的单价" v-model="price" @input="change">
					<text>￥</text>
				</view>
				<view class="cost">
					<view>支付通道服务费{{payment_channel}}%：￥{{payment_price}}</view>
					<view>预计可获得：￥{{result}}</view>
				</view>
				<view class="password">
					<view>请输入交易密码</view>
					<input password type="text" placeholder="请输入交易密码" v-model="password">
				</view>
				<view class="btn">
					<view @click="submit">确认转售</view>
				</view>
			</view>
		</scroll-view>
		<u-popup :show="popup_show" bgColor="transparent" mode="center" @close="close">
			<SellFail :text="text" @close="close" @confirm="popup=2" v-if="popup==1" />
			<SellSuccess @close="close" v-if="popup==2" />
		</u-popup>
	</view>
</template>

<script>
	import {
		orderInfo,
		orderSell
	} from '@/request/api.js';
	import SellFail from '@/components/market/SellFail.vue';
	import SellSuccess from '@/components/market/SellSuccess.vue';
	export default {
		components: {
			SellFail,
			SellSuccess
		},
		data() {
			return {
				id: null,

				cover: "",
				name: "",
				number: "",
				price: "",
				password: "",

				payment_channel: 0,
				payment_price: 0,
				result: 0,

				text: "交易密码错误,请重新输入",
				popup_show: false,
				popup: 1
			}
		},
		onLoad(option) {
			this.id = option.id;
			this.getDeta();
		},
		methods: {
			async getDeta() {
				let {
					info,
					pay_mode,
					setting
				} = await orderInfo({
					id: this.id
				});

				this.cover = info.product.cover;
				this.name = info.product.name;
				this.number = info.product_number;
				this.payment_channel = setting.payment_channel;
			},
			change(e) {
				let price = e.target.value.replace(/[^\d.]/g, '').replace(/(\d)(\.)(\d*)(\2*)(\d*)/, '$1$2$3$5').replace(
					/^(\-)*(\d+)\.(\d{1,4}).*$/, '$1$2.$3');
				this.$nextTick(() => {
					this.price = price;
				});
				let payment_price = this.payment_price = (price * Number(this.payment_channel) / 100).toFixed(4);
				this.result = price - payment_price;
			},
			async submit() {
				await orderSell({
					id: this.id,
					price: this.price,
					pay_password: this.password
				}, {
					custom: {
						catch: true,
						toast: false
					}
				}).then(() => {
					this.open(2)
				}).catch(err => {
					this.text = err.message;
					this.open(1);
				});
				// this.$u.toast("操作成功");
				// setTimeout(() => {
				// 	this.backRouter();
				// }, 1000);
				// this.open(2)
			},
			open(index) {
				this.popup = index;
				this.popup_show = true;
			},
			close() {
				this.popup_show = false;
			}
		}
	}
</script>

<style scoped lang="scss">
	.sell {
		height: 100vh;
		background: #29170D;
		overflow: hidden;

		.scroll_view {
			height: calc(100vh - #{$navbarHeight});
			padding: 30rpx 25rpx;
			box-sizing: border-box;

			.info {
				width: 100%;
				height: 288rpx;
				padding: 44rpx 40rpx;
				box-sizing: border-box;
				background: url(#{$baseImageUrl}/bg/700_288.png) no-repeat;
				background-size: 100% 100%;
				display: flex;

				.cover {
					width: 200rpx;
					height: 200rpx;
				}

				.info_view {
					flex: 1;
					padding: 20rpx 0 0 34rpx;
					box-sizing: border-box;
					overflow: hidden;

					.name {
						line-height: 50rpx;
						color: #494338;
						font-size: 50rpx;
						font-weight: 400;
						font-family: LiSu;
					}

					.number {
						line-height: 40rpx;
						color: #494338;
						font-size: 40rpx;
						font-weight: 400;
						font-family: LiSu;
						margin-top: 40rpx;
					}
				}
			}

			.content {
				width: 100%;
				height: 618rpx;
				margin-top: 10rpx;
				padding: 48rpx 60rpx;
				box-sizing: border-box;
				background: url(#{$baseImageUrl}/bg/700_618.png) no-repeat;
				background-size: 100% 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.title {
					margin-bottom: 30rpx;
					display: flex;
					justify-content: center;
					align-items: center;

					view {
						height: 30rpx;
						display: flex;
						align-items: center;

						image {
							width: 30rpx;
							height: 30rpx;
						}

						text {
							color: #494338;
							font-size: 42rpx;
							font-weight: 400;
							font-family: LiSu;
							margin: 0 10rpx;
						}
					}
				}

				.price {
					width: 100%;
					height: 80rpx;
					border-radius: 10rpx;
					box-shadow: 2rpx -1px 10rpx 0px rgba(3, 0, 0, 0.2);
					background: #C0B5B6;
					display: flex;
					align-items: center;

					input {
						flex: 1;
						padding-left: 34rpx;
						box-sizing: border-box;
					}

					text {
						width: 110rpx;
						text-align: center;
						color: #494338;
						font-size: 28rpx;
						font-weight: 400;
						font-family: SimSun;
					}
				}

				.cost {
					view {
						color: #494338;
						font-size: 32rpx;
						font-weight: 400;
						font-family: SimSun;
						margin: 10rpx 0;
					}
				}

				.password {
					view {
						color: #494338;
						font-size: 32rpx;
						font-weight: 400;
						font-family: SimSun;
						margin-bottom: 20rpx;
					}

					input {
						height: 80rpx;
						border-radius: 10rpx;
						padding: 0 45rpx;
						box-sizing: border-box;
						box-shadow: 2rpx -1px 10rpx 0px rgba(3, 0, 0, 0.2);
						background: #C0B5B6;
					}
				}

				.btn {
					display: flex;
					justify-content: center;

					view {
						width: 204rpx;
						height: 70rpx;
						line-height: 70rpx;
						text-align: center;
						color: #FFFFFF;
						font-size: 30rpx;
						font-weight: 400;
						font-family: SimSun;
						background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
						background-size: 100% 100%;
					}
				}
			}
		}
	}
</style>